#!/bin/bash

DATA_DIR=/data/app
HOST_IP=127.0.0.1
alias cp='cp'
IPS=$(ip a |grep -vE 'docker[0-9]|veth|br-' |grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' |grep -E '^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$')
ScriptFolder=$(dirname $(readlink -f $0))

if [[ -z "${ScriptFolder}" ]];then
    echo "\${ScriptFolder}获取为空..."
    exit 1
fi


function check_ip() {
    local IP=$1
    VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $IP |grep -E "^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$" >/dev/null; then
        if [[ $VALID_CHECK == "yes" ]]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

function choice_ip() {
    echo "本机所检测到的IP如下:"
    echo "${IPS[@]}"
    while :; do
        read -p "请输入IP地址（如果需要公网，自行配置映射即可）: " IP
        check_ip ${IP}
        if [[ $? -ne 0 ]]; then
          echo "---输入的地址格式有误，请重新输入IP或域名。"
        else
          HOST_IP=${IP}
          break
        fi
    done
}

choice_ip

cd ${DATA_DIR}

yum install git -y

echo "克隆snmp-exporter"
git clone https://gitee.com/yadeno/snmp-exporter.git

if [[ ! -d ${DATA_DIR}/snmp-exporter ]];then
  echo "git clone snmp-exporter 失败"
  exit 1
fi

chmod +x /data/app/snmp-exporter/snmp_exporter

echo "复制报警规则文件到promethues"
cp -rfp ${DATA_DIR}/snmp-exporter/idrac.yml ${DATA_DIR}/prometheus/etc/rules/

echo "生成snmp-exporter target配置模板"
if [[ -f ${DATA_DIR}/prometheus/etc/configs/idrac-tz.json ]];then
echo "${DATA_DIR}/prometheus/etc/configs/idrac-tz.json己存在，将不会创建"
else
cat <<EOF > ${DATA_DIR}/prometheus/etc/configs/idrac-tz.json
[
  {
    "targets": [
      # IDRAC的SNMP地址，通过公网映射出来的
      "tz1.abc.com:10161"
    ],
    "labels": {
      # 区分
      "zone": "TZ",
      # 主机名，用于分别IDRAC主机是哪一台
      "hostname": "tz01-abc01",
      "collection": "snmp-exporter"
    }
  }
]
EOF
echo "创建了以下: ${DATA_DIR}/prometheus/etc/configs/idrac-tz.json，配置模板，自行添加主机。"
fi

echo "生成systemctl服务"
cat > /etc/systemd/system/snmp-exporter.service <<EOF
[Unit]
Description=Promethues Snmp Exporter
After=network.target

[Service]
Type=simple
ExecStart=${DATA_DIR}/snmp-exporter/snmp_exporter --config.file="${DATA_DIR}/snmp-exporter/dell-snmp.yml" --web.listen-address="${HOST_IP}:19116"
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable snmp-exporter
systemctl start snmp-exporter

echo "服务安装完成..."

echo "手动将以下配置添加到prometheus配置文件中，如果已经添加过可以忽略"
echo "注意格式，YAML不要使用tab，一定要使用空格缩进，2个空格一缩进"
echo "
  # IDRAC SNMP
  - job_name: 'IDRAC'
    scrape_interval: 180s
    scrape_timeout: 180s
    # 动态读取以idarc-开头的json，多配置
    file_sd_configs:
      - files:
        - configs/idrac-*.json
    metrics_path: /snmp
    params:
      module: [dell]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        # snmp_exporter的IP:PORT
        replacement: ${HOST_IP}:19116
"
echo "添加完成之后，重启prometheus容器。"